package com.wendu.oa.leave.api.leave;

import com.wendu.oa.common.Constants;
import com.wendu.oa.common.CurrentUser;
import com.wendu.oa.common.Result;
import com.wendu.oa.common.TokenUtils;
import com.wendu.oa.common.exception.BusinessException;
import com.wendu.oa.leave.dto.LeaveDto;
import com.wendu.oa.leave.model.Leave;
import com.wendu.oa.leave.service.leave.ApprevService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/apprev")
public class ApprevController {

    @Autowired
    private ApprevService apprevService;

    @GetMapping("/hello")
    public String hello(){
        return "hello!";
    }

    @GetMapping("/app")
    public Result appList(HttpServletRequest request,LeaveDto leaveDto){


        String token = request.getHeader(Constants.HEADER_TOKEN);
        CurrentUser currentUser = TokenUtils.getCurrentUser(token);
        leaveDto.setE_id(currentUser.getUserId());



        Map<String,Object> page = apprevService.getAppList(leaveDto);
        return Result.OK(page);
    }

    @PostMapping("/app")
    public Result applyLeave(@RequestBody LeaveDto leaveDto, HttpServletRequest request){

        String token = request.getHeader(Constants.HEADER_TOKEN);
        CurrentUser currentUser = TokenUtils.getCurrentUser(token);
        leaveDto.setE_id(currentUser.getUserId());

        apprevService.saveNewLeave(leaveDto);

        return Result.OK();

    }
    @PutMapping("/app/commit/{l_id}")
    public Result commitLeave(@PathVariable String l_id, HttpServletRequest request){
        try {
            String token = request.getHeader(Constants.HEADER_TOKEN);
            CurrentUser currentUser = TokenUtils.getCurrentUser(token);

            LeaveDto leaveDto = new LeaveDto();
            leaveDto.setL_id(l_id);
            leaveDto.setE_id(currentUser.getUserId());

            apprevService.commitLeave(leaveDto);

            return Result.OK();
        } catch (BusinessException e) {
            return Result.err(Result.CODE_ERR_BUSINESS, e.getMessage());

        }catch(Exception e){
            log.error("系统错误！",e);
            return Result.err(Result.CODE_ERR_SYS, e.getMessage());
        }
    }

    //销假
    @PutMapping("/app/rev/{l_id}")
    public Result revLeave(@PathVariable String l_id, HttpServletRequest request){
        try {
            String token = request.getHeader(Constants.HEADER_TOKEN);
            CurrentUser currentUser = TokenUtils.getCurrentUser(token);

            LeaveDto leaveDto = new LeaveDto();
            leaveDto.setL_id(l_id);
            leaveDto.setE_id(currentUser.getUserId());

            apprevService.revLeave(leaveDto);

            return Result.OK();
        } catch (BusinessException e) {
            return Result.err(Result.CODE_ERR_BUSINESS, e.getMessage());

        }catch(Exception e){
            log.error("系统错误！",e);
            return Result.err(Result.CODE_ERR_SYS, e.getMessage());
        }
    }





}
